/*
 ------------------------------------------------------------------------------
 Copyright (C) 2013 Eternal Games.

 This file is part of the EternalTech source code.

 The EternalTech source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The EternalTech source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the EternalTech source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// wad.cpp - WAD2 format loading
//

// TODO:
// - support for loading other wads?
// - add console commands for listing stuff


#include "common.h"


struct wad_t {
	byte *					data;

	int						numLumps;
	wadLump_t *				lumps;
};

static wad_t				wad;


/*
 ==================
 W_FixName

 Lowercases name and pads with spaces and a terminating 0 to the length of
 lumpinfo_t->name.

 Used so lump name lookups can proceed rapidly by comparing 4 chars at a time
 Space padding is so names can be printed nicely in tables.

 Can safely be performed in place.
 ==================
*/
static void W_FixName (const char *in, char *out){

	int		c;
	int		i;

	for (i = 0; i < 16; i++){
		c = in[i];
		if (!c)
			break;

		if (c >= 'A' && c <= 'Z')
			c += ('a' - 'A');

		out[i] = c;
	}

	for ( ; i < 16; i++)
		out[i] = 0;
}

/*
 ==================
 W_LoadWAD

 FIXME: this could be written better...
 ==================
*/
static bool W_LoadWAD (const char *name){

	wadHeader_t	*header;
	wadLump_t	*lump;
	wadPic_t	*pic;
	int			infoTableOfs;
	int			i;

	// Load the file
	FS_ReadFile(name, (void **)&wad.data);
	if (!wad.data)
		return false;

	// Parse the WAD2 header
	header = (wadHeader_t *)wad.data;

	if (header->id[0] != 'W' || header->id[1] != 'A' || header->id[2] != 'D' || header->id[3] != '2')
		Com_Error(true, "W_LoadWAD: wrong file id (%s)", name);

	wad.numLumps = LittleLong(header->numLumps);
	infoTableOfs = LittleLong(header->infoTableOfs);
	wad.lumps = (wadLump_t *)(wad.data + infoTableOfs);

	// Swap lump bytes
	for (i = 0, lump = wad.lumps; i < wad.numLumps; i++, lump++){
		lump->filePos = LittleLong(lump->filePos);
		lump->size = LittleLong(lump->size);

		W_FixName(lump->name, lump->name);

		if (lump->type == TYP_QPIC){
			pic = (wadPic_t *)(wad.data + lump->filePos);

			pic->width = LittleLong(pic->width);
			pic->height = LittleLong(pic->height);	
		}
	}

	// Free file data
//	FS_FreeFile(data);

	return true;
}


// ============================================================================


/*
 ==================
 W_GetLumpData
 ==================
*/
wadLump_t *W_GetLumpData (const char *name){

	wadLump_t	*lump;
	char		fixedName[16];
	int			i;

	W_FixName(name, fixedName);

	for (i = 0, lump = wad.lumps; i < wad.numLumps; i++, lump++){
		if (!Str_ICompare(lump->name, fixedName))
			return lump;
	}

	Com_Error(true, "W_GetLumpData: %s not found", name);

	return NULL;
}

/*
 ==================
 W_GetLump

 TODO: might want to check if name and lump is valid
 ==================
*/
void *W_GetLump (const char *name){

	wadLump_t	*lump;

	lump = W_GetLumpData(name);
	if (!lump)
		return NULL;

	return (void *)(wad.data + lump->filePos);
}


/*
 ==============================================================================

 INITIALIZATION & SHUTDOWN

 ==============================================================================
*/


/*
 ==================
 W_Init
 ==================
*/
void W_Init (){

	// Load the file
	if (!W_LoadWAD("gfx.wad"))
		Com_Error(true, "W_Init: gfx.wad not found");
}

/*
 ==================
 W_Shutdown
 ==================
*/
void W_Shutdown (){

}